<?php

namespace App\Admin\Controllers;

use App\Models\BankList;
use App\Models\CollPosition;
use App\Models\ProductAttrVal;
use App\Models\UserVideos;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Redis;


class UserVideosController extends AdminController
{
    protected $title = '用户视频列表';

    protected static $baseImg = 'https://winesh.oss-cn-beijing.aliyuncs.com';

    protected function grid()
    {
        $grid = new Grid(new UserVideos());

        $grid->model()->orderBy('id', 'desc');

        $grid->column('id', __('Id号'));
        $grid->column('type', __('资源类型'))->using(['1' => '图片', '2' => '视频']);
        $grid->column('content', __('介绍说明'));
        $grid->column('cover_img', __('封面图'))->image();
        $grid->column('status', __('状态'))->using(['0' => '待审核', '1' => '正常']);

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
        });

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $newArr = [];
            $worksArr = CollPosition::where('grade', 2)->get()->toArray();
            foreach ($worksArr as $v) {
                $newArr[$v['cid']] = $v['name'];
            }

            $filter->in('job_category1', '工种')->multipleSelect($newArr);
        });

        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableColumnSelector();

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(UserVideos::findOrFail($id));

        $type = UserVideos::where('id', $id)->value('type');

        $show->field('type', __('资源类型'))->using(['1' => '图片', '2' => '视频']);
        $show->field('content', __('介绍说明'));
        $show->field('cover_img', __('封面图'))->image();

        if ($type == 1) {
            $show->field('srcs', __('图集'))->carousel();
        } elseif ($type == 2) {
            $show->field('srcs', __('视频'))->unescape()->as(function ($value) {
                return "<video src=" . $value . " controls=\"controls\"></video>";
            });
        }

        $show->field('status', __('状态'))->using(['0' => '待审核', '1' => '正常']);

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new UserVideos());

        $form->select('type', __('资源类型'))->options(['1' => '图片', '2' => '视频'])->readonly();
        $form->textarea('content', __('介绍说明'))->readonly();
        $form->image('cover_img', __('封面图'));
//
        if ($form->isEditing()) {
            $id = request()->route()->parameters()["user_video"];
            $type = UserVideos::where('id', $id)->value('type');
            if ($type == 1) {
                $form->image('srcs', __('图集'));
            } elseif ($type == 2) {
                $form->display('srcs', __('视频'))->with(function ($value) {
                    return "<video src=" . $value . " controls=\"controls\"></video>";
                });
            }
        };

        $form->radio('status', __('状态'))->options(['0' => '待审核', '1' => '正常'])->default('0');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}